package cn.u313.timer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import cn.u313.comm.conststring.粽子;
import cn.u313.model.Duty;
import com.alibaba.fastjson.JSONObject;
import love.forte.common.ioc.annotation.Beans;
import love.forte.simbot.bot.BotManager;
import me.kuku.test.QQlogin;
import me.kuku.utils.MyUtils;
import me.kuku.utils.OkHttpUtils;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Beans
@Component
public class Scheduler{
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    /**
     * 获取bot管理器。
     */
    @Autowired
    private BotManager botManager;
    //每隔2秒执行一次
//    @Scheduled(fixedRate = 2000)
//    public void testTasks() {
//        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
//    }
//
    private static String url = "https://u6.y.qq.com/cgi-bin/musicu.fcg?format=json&inChars" +
            "et=utf8&outCharset=utf-8&data=%7B%22req1%22%3A%7B%22module%2" +
            "2%3A%22QQConnectLogin.LoginServer%22%2C%22method%22%3A%22" +
            "QQLogin%22%2C%22param%22%3A%7B%22expired_in%22%3A7776000%" +
            "2C%22musicid%22%3Aqquser%2C%22musickey%22%3A%22token%22%7D%7D%7D";
    //每天3：05执行
    @Scheduled(cron = "0 38 13 ? * *")
    public void dingshi() {
        new Thread(() -> {
            try {
                String str = OkHttpUtils.get("https://plugin.313u.cn//cgi_bin/rex/get?a=test").body().string();
                JSONObject parse = JSONObject.parseObject(str);
                String authst = parse.getString("authst");
                String qq = parse.getString("user");
                String url = this.url.replace("qquser",qq)
                        .replace("token",authst);
                String strToken = OkHttpUtils.get(url).body().string();
                String token = MyUtils.regex("\"musickey\":\"","\",\"musickeyCreateTime",strToken);
                if (token == null || token.length()<7) {
                    throw new NullPointerException(strToken);
                }

                Map<String, String> map = new HashMap<>();
                map.put("qq", qq);
                String ui = UUID.randomUUID().toString();
                map.put("ui", ui);
                map.put("key", token);
                map.put("userToken", "1054980699");
                Response response = OkHttpUtils.post("http://plugin.313u.cn/cgi_bin/rex/savefn1235444",
                        map);
                String str1 = OkHttpUtils.getStr(response);
                botManager.getBots().get(0).getSender().SENDER.sendPrivateMsg(1054980699l,"更新成功！\ntoken:"+ token.substring(0,7)+"\n返回值："+str1);

            } catch (Exception e) {
                botManager.getBots().get(0).getSender().SENDER.sendPrivateMsg(1054980699l,e.getMessage());
                e.printStackTrace();
            }
            System.out.println("定时！");

        }).start();
    }

    public static void main(String[] args) {
        try {
            String str = OkHttpUtils.get("https://plugin.313u.cn//cgi_bin/rex/get?a=test").body().string();
            JSONObject parse = JSONObject.parseObject(str);
            String authst = parse.getString("authst");
            String qq = parse.getString("user");
            String url0 = url.replace("qquser",qq)
                    .replace("token",authst);
            String strToken = OkHttpUtils.get(url0).body().string();
            String token = MyUtils.regex("\"musickey\":\"","\",\"musickeyCreateTime",strToken);
            if (token == null || token.length()<7) {
                throw new NullPointerException(strToken);
            }
            System.out.println(token);
//            botManager.getBots().get(0).getSender().SENDER.sendPrivateMsg(1054980699l,"更新成功！\ntoken:"+ token.substring(0,7));

        } catch (Exception e) {
//            botManager.getBots().get(0).getSender().SENDER.sendPrivateMsg(1054980699l,e.getMessage());
            e.printStackTrace();
        }
    }
    //每天3：05执行
//    @Scheduled(cron = "0 1 7 ? * *")
//    public void 打扫() {
//        botManager.getBots().get(0).getSender()
//                .SENDER.sendGroupMsg(772185038l, Duty.next());
//    }

    public static boolean error = false;
    @Scheduled(cron = "0 */1 * * * ?")
    public void get124Ip(){
        String url = "http://plugin.313u.cn/";
        try {
            int i = OkHttpUtils.get(url).code();
            System.out.println("APP服务器状态："+i);
            if (i>500&&!error){
                error = true;
                botManager.getBots().get(0).getSender().SENDER.sendPrivateMsg(1054980699l,"124服务器状态 【 502 】");
                return;
            }

            error = false;
        } catch (IOException e) {
            if (!error){
                error = true;
                botManager.getBots().get(0).getSender()
                        .SENDER.sendPrivateMsg(1054980699l,"124服务器状态 【 502 】");
                return;
            }
            e.printStackTrace();
        }
        error = false;
    }
    //0 0 20 * * ?

//    @Scheduled(cron = "0 0 20 * * ?")
//    public void executeZz(){
//
//        粽子.zzRo1 = 粽子.toZongZir();
//        粽子.zzRo2 = 粽子.toZongZir();
//        粽子.zzRo3 = 粽子.toZongZir();
//        if(粽子.zzRo2.equals(粽子.zzRo1)){
//            粽子.zzRo2 = 粽子.toZongZir();
//        }else if(粽子.zzRo3.equals(粽子.zzRo1)){
//            粽子.zzRo3 = 粽子.toZongZir();
//        }else if(粽子.zzRo2.equals(粽子.zzRo3)){
//            粽子.zzRo3 = 粽子.toZongZir();
//        }
//        System.out.println(粽子.zzRo1+
//                粽子.zzRo2+
//                粽子.zzRo3);
//    }
//    @Scheduled(cron = "0 0 0 * * ?")
//    public void executeZzclear(){
//
//        粽子.zzRo1 = null;
//        粽子.zzRo2 = null;
//        粽子.zzRo3 = null;
//
//    }
}